package web.servlet;

import domain.*;
import service.ArticleService;
import service.impl.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

@WebServlet("/article/*")
public class ArticleServlet extends BaseServlet {
    private ArticleService service = new ArticleServiceImpl();

    public void pageQuery(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String currentPage = request.getParameter("currentPage");
        String rows = request.getParameter("rows");
        if (currentPage == null || "".equals(currentPage)) {
            currentPage = "1";
        }
        if (rows == null || "".equals(rows)) {
            rows = "10";
        }
        String code = null;
        int cid = 1; //如果没有找到默认文章分类id为1
        //获取条件查询参数
        Map<String, String[]> condition = request.getParameterMap();
        Set<String> keySet = condition.keySet();
        List<ArticleDetail> list = new ArrayList();
        AdminServiceImpl adminService = new AdminServiceImpl();//直接复用admin查询文章信息的方法
        CategoryServiceImpl categoryService = new CategoryServiceImpl();
        for (String key : keySet) {
            String value = condition.get(key)[0];
            if (key != "" && key.equals("lid") && !"".equals(value)) {
                PageBean pb = service.findLabelByLid(currentPage, rows, Integer.parseInt(value));
                List<ArticleLabel> list2 = pb.getList(); // 获取集合元素列表
                for (ArticleLabel articleLabel : list2) {
                    code = articleLabel.getCode();
                    Article article = service.findArticleByRid(articleLabel.getRid());
                    User user = adminService.findUserByCode(code);
                    Category category = adminService.findCategory(article.getCid());
                    ArticleDetail<ArticleDetail> ad = new ArticleDetail<ArticleDetail>();
                    ad.setUsername(user.getUsername());
                    ad.setCname(category.getCname());
                    ad.setTitle(article.getTitle());
                    ad.setCreateTime(article.getCreateTime());
                    ad.setModifyTime(article.getModifyTime());
                    ad.setBrowserNum(article.getBrowserNum());
                    ad.setRid(article.getRid());
                    ad.setFavour(article.getFavour());
                    list.add(ad);
                    System.out.println(list + "------------->admin/pageQuery");
                    request.getSession().setAttribute("list", list); //文章信息
                    request.getSession().setAttribute("pb", pb);

                    categoryService = new CategoryServiceImpl();
                    List<Category> categoryList = categoryService.findAll();
                    LabelServiceImpl labelService = new LabelServiceImpl();
                    List<Label> labelList = labelService.findAll();
                    request.getSession().setAttribute("categoryList", categoryList);
                    request.getSession().setAttribute("labelList", labelList);

                    request.getRequestDispatcher("/articleCenter.jsp").forward(request, response);
                }
                return;
            }

        }
        // 不只存在一个特定的键
        PageBean pb = service.pageBean(currentPage, rows, condition);
        List<Article> list2 = pb.getList(); // 获取集合元素列表
        for (Article article : list2) {//遍历文章表中的cid和code
            cid = article.getCid();
            code = article.getCode();
            User user = adminService.findUserByCode(code);
            Category category = adminService.findCategory(cid);
            ArticleDetail<ArticleDetail> ad = new ArticleDetail<>();
            ad.setUsername(user.getUsername());
            ad.setCname(category.getCname());
            ad.setTitle(article.getTitle());
            ad.setCreateTime(article.getCreateTime());
            ad.setModifyTime(article.getModifyTime());
            ad.setBrowserNum(article.getBrowserNum());
            ad.setRid(article.getRid());
            ad.setFavour(article.getFavour());
            list.add(ad);
        }
        System.out.println(list + "------------->admin/pageQuery");
        request.getSession().setAttribute("list", list);
        request.getSession().setAttribute("pb", pb);

        System.out.println(pb + "------------>article/pageQuery");
        List<Category> categoryList = categoryService.findAll();
        LabelServiceImpl labelService = new LabelServiceImpl();
        List<Label> labelList = labelService.findAll();
        request.getSession().setAttribute("categoryList", categoryList);
        request.getSession().setAttribute("labelList", labelList);

        request.getRequestDispatcher("/articleCenter.jsp").forward(request, response);
    }

    public void seekGoodArticle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String currentPage = request.getParameter("currentPage");
        String rows = request.getParameter("rows");
        if (currentPage == null || "".equals(currentPage)) {
            currentPage = "1";
        }
        if (rows == null || "".equals(rows)) {
            rows = "10";
        }
        String code = null;
        int cid = 1; //如果没有找到默认文章分类id为1
        PageBean pb = service.findArticleByPage(currentPage, rows);//所有文章信息
        List<ArticleDetail> list = new ArrayList();
        List<Article> list2 = pb.getList(); // 获取集合元素列表
        for (Article article : list2) {//遍历文章表中的cid和code
            cid = article.getCid();
            code = article.getCode();
            User user = service.findUserByCode(code);
            Category category = service.findCategory(cid);
            ArticleDetail<ArticleDetail> ad = new ArticleDetail<>();
            ad.setUsername(user.getUsername());
            ad.setCname(category.getCname());
            ad.setTitle(article.getTitle());
            ad.setFavour(article.getFavour());
            ad.setCreateTime(article.getCreateTime());
            ad.setModifyTime(article.getModifyTime());
            ad.setBrowserNum(article.getBrowserNum());
            ad.setRid(article.getRid());
            list.add(ad);
        }
        System.out.println(list + "------------->article/seekGoodArticle");
        request.getSession().setAttribute("list", list);
        request.getSession().setAttribute("pb", pb);
        request.getRequestDispatcher("/goodArticles.jsp").forward(request, response);
    }


    public void lookArticle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String rid = request.getParameter("rid");
        Article article = service.findArticleByRid(Integer.parseInt(rid));
        User user = service.findUserByCode(article.getCode());
        request.getSession().setAttribute("article", article); //展示文章中心和精选文章的文章内容
        request.getSession().setAttribute("otherUser", user); //展示文章中心和精选文章的文章内容
        request.getSession().setAttribute("message","点赞");
        service.addBrowserNum(Integer.parseInt(rid)); //浏览量加一
        request.getRequestDispatcher("/articleShow.jsp").forward(request, response);
    }
}
